package au.com.handsunited.web.controller.organisation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;

import au.com.handsunited.model.BaseEntity;
import au.com.handsunited.model.Organisation;
import au.com.handsunited.model.invitations.Invitation;
import au.com.handsunited.service.OrganisationService;
import au.com.handsunited.web.command.organisation.InviteToOrganisationCommand;
import au.com.handsunited.web.controller.entity.AbstractInviteToController;
import au.com.handsunited.web.session.SessionSupport;

/**
 * @author arunda
 *
 */
public class InviteToOrganisationController extends AbstractInviteToController {

	private OrganisationService organisationService;


	private static Logger logger = LoggerFactory.getLogger(InviteToOrganisationController.class);

	/**
	 *
	 */
	public InviteToOrganisationController() {
		super();
		setCommandClass(InviteToOrganisationCommand.class);
	}


	/**
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {

		InviteToOrganisationCommand inviteToOrganisationCommand = (InviteToOrganisationCommand) command;
		ModelAndView mav = new ModelAndView(getSuccessView());

		if (SessionSupport.getCurrentlyActiveEntity(request).isOrganisation()) {

			List<BaseEntity> invitedUsers = getInvitedEntities(
					inviteToOrganisationCommand.getUserMap(), inviteToOrganisationCommand.getUserIds());

			Map<String, List<Invitation>> invitationsMap = organisationService.inviteToOrganisation(
					(Organisation)SessionSupport.getCurrentlyActiveEntity(request), SessionSupport.getUser(request),
					invitedUsers, inviteToOrganisationCommand.getEmailAddresses(),
					inviteToOrganisationCommand.getMessage());

			List<String> validEmailAddresses = getEmailAddressesFromInvitations(invitationsMap.get(Invitation.VALID_EMAILADDRESSES));
			List<String> invalidEmailAddresses = getEmailAddressesFromInvitations(invitationsMap.get(Invitation.INVALID_EMAILADDRESSES));
			List<String> invitedEntites = getInviteeNamesFromInvitations(invitationsMap.get(Invitation.INVITED_ENTITIES));

			Map<String, String> model = new HashMap<String, String>();

			model.put(Invitation.INVALID_EMAILADDRESSES, StringUtils.join(invalidEmailAddresses, ", "));
			model.put(Invitation.VALID_EMAILADDRESSES, StringUtils.join(validEmailAddresses, ", "));
			model.put(Invitation.INVITED_ENTITIES, StringUtils.join(invitedEntites, ", "));

			mav = new ModelAndView(getSuccessView(), model);
		}

		return mav;
	}


	/**
	 * @see au.com.handsunited.web.controller.entity.AbstractInviteToController#referenceData(javax.servlet.http.HttpServletRequest, java.lang.Object, org.springframework.validation.Errors)
	 */
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {

		Map<String, Object> data = super.referenceData(request, command, errors);
		data.put("organisation", SessionSupport.getCurrentlyActiveEntity(request));
		return data;
	}


	/**
	 * @param organisationService the organisationService to set
	 */
	public void setOrganisationService(OrganisationService organisationService) {
		this.organisationService = organisationService;
	}

}

